
#include "semantics_field.h"
#include "generic_table.cpp"
#include "main_container.h"
#include "semantic_analyzer.h"
extern MainContainer * CompilerData;


SemanticsFieldSingle::SemanticsFieldSingle()
{
	this->CurrentContext = -1;

	this->ModifierConst = false;
	this->ModifierPrivate = false;
	this->ModifierProtected = false;
	this->ModifierPublic = false;
	this->ModifierStatic = false;

	this->DataType = new SemanticsDataType();
	this->ArrayCount = -1;

	this->Expression = NULL;
	this->Name = new GenericTable<int>();
}


SemanticsFieldSingle::~SemanticsFieldSingle()
{
	if(Expression != NULL)
	{
		delete Expression;
		Expression = NULL;
	}

	if(Name != NULL)
	{
		delete Name;
		Name = NULL;
	}

	if(DataType != NULL)
	{
		delete DataType;
		DataType = NULL;
	}
}



#ifdef DEBUG
void SemanticsFieldSingle::DebugXMLOutput(FILE * fo)
{
	fprintf(fo, "<semanticsFieldSingle name=\"");
	SemanticAnalyzer::DebugOutputNameFromTable(fo, this->Name, CompilerData->Units);

	fprintf(fo, "\" currentContext=\"");
	fprintf(fo, "%d", this->CurrentContext);

	fprintf(fo, "\" arrayCount=\"");
	fprintf(fo, "%d", this->ArrayCount);

	fprintf(fo, "\" modifierPrivate=\"");
	if (this->ModifierPrivate)
		fprintf(fo, "true");
	else
		fprintf(fo, "false");

	fprintf(fo, "\" modifierPublic=\"");
	if (this->ModifierPublic)
		fprintf(fo, "true");
	else
		fprintf(fo, "false");

		fprintf(fo, "\" modifierProtected=\"");
	if (this->ModifierProtected)
		fprintf(fo, "true");
	else
		fprintf(fo, "false");

			fprintf(fo, "\" modifierStatic=\"");
	if (this->ModifierStatic)
		fprintf(fo, "true");
	else
		fprintf(fo, "false");

	
		fprintf(fo, "\" modifierConst=\"");
	if (this->ModifierConst)
		fprintf(fo, "true");
	else
		fprintf(fo, "false");

	fprintf(fo, "\">\n");


	this->Expression->DebugXMLOutput(fo);


	fprintf(fo, "</semanticsFieldSingle>\n");
}
#endif



SemanticsFieldVariable::SemanticsFieldVariable()
{
	this->ArrayCount = -1;
	this->CurrentContext = -1;

	this->Expression = NULL;
	this->Name = new GenericTable<int>;
}



SemanticsFieldVariable::~SemanticsFieldVariable()
{
	if(Expression != NULL)
	{
		delete Expression;
		Expression = NULL;
	}

	if(Name != NULL)
	{
		delete Name;
		Name = NULL;
	}
}


#ifdef DEBUG
void SemanticsFieldVariable::DebugXMLOutput(FILE * fo)
{
	fprintf(fo, "<semanticsFieldVariable name=\"");
	SemanticAnalyzer::DebugOutputNameFromTable(fo, this->Name, CompilerData->Units);

	fprintf(fo, "\" currentContext=\"");
	fprintf(fo, "%d", this->CurrentContext);

	fprintf(fo, "\" arrayCount=\"");
	fprintf(fo, "%d", this->ArrayCount);

	this->Expression->DebugXMLOutput(fo);

	fprintf(fo, "\" />\n");
}
#endif


SemanticsField::SemanticsField()
{
	this->CurrentContext = -1;

	this->ModifierConst = false;
	this->ModifierPrivate = false;
	this->ModifierProtected = false;
	this->ModifierPublic = false;
	this->ModifierStatic = false;

	this->DataType = new SemanticsDataType();
	this->Variables = new GenericTable<SemanticsFieldVariable>();
}


SemanticsField::~SemanticsField()
{
	if(DataType != NULL)
	{
		delete DataType;
		DataType = NULL;
	}

	if(Variables != NULL)
	{
		delete Variables;
		Variables = NULL;
	}
}



#ifdef DEBUG
void SemanticsField::DebugXMLOutput(FILE * fo)
{
	fprintf(fo, "<semanticsField currentContext=\"");
	fprintf(fo, "%d", this->CurrentContext);

	fprintf(fo, "\" modifierPrivate=\"");
	if (this->ModifierPrivate)
		fprintf(fo, "true");
	else
		fprintf(fo, "false");

	fprintf(fo, "\" modifierPublic=\"");
	if (this->ModifierPublic)
		fprintf(fo, "true");
	else
		fprintf(fo, "false");

		fprintf(fo, "\" modifierProtected=\"");
	if (this->ModifierProtected)
		fprintf(fo, "true");
	else
		fprintf(fo, "false");

			fprintf(fo, "\" modifierStatic=\"");
	if (this->ModifierStatic)
		fprintf(fo, "true");
	else
		fprintf(fo, "false");

	
		fprintf(fo, "\" modifierConst=\"");
	if (this->ModifierConst)
		fprintf(fo, "true");
	else
		fprintf(fo, "false");

	fprintf(fo, "\">\n");


	// make output of table Variables
	fprintf(fo, "<variables>\n");
	for(int i = 0; i < this->Variables->GetCount(); i++)
	{
		SemanticsFieldVariable * data = this->Variables->GetData(i);
		data->DebugXMLOutput(fo);
	}
	fprintf(fo, "</variables>\n");

	fprintf(fo, "</semanticsField>\n");
}
#endif
